package com.ogawa.chair7808.widget

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.LogUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_UPDATE_TIME
import com.ogawa.base.utils.StringUtils
import com.ogawa.chair7808.cache.deviceStatusBean
import com.ogawa.chair7808.cache.hasProgramRunning
import com.ogawa.chair7808.manager.DataManager
import com.ogawa.chair7808.manager.TimeBean
import com.ogawa.chair7808.ui.activity.BaseActivity
import java.util.*

class ViewTvTime @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : AppCompatTextView(context, attrs, defStyleAttr) {
    private val baseActivity = context as BaseActivity
    override fun onFinishInflate() {
        super.onFinishInflate()
        if(hasProgramRunning){
            deviceStatusBean?.let {
                if (it.timeRemaining.isNotEmpty()) {
                    val split = it.timeRemaining.split(":")
                    LogUtils.e("initTime1122==${split[0].toLong()}:${split[1].toLong()}")
                    text = "" + StringUtils.numberToTime(split[0]) + " : " + StringUtils.numberToTime(split[1])

                }
            }
        }
        else{
            text = "00 : 00"

        }



        LiveEventBus.get(LIVE_EVENT_BUS_KEY_UPDATE_TIME, TimeBean::class.java).observe(baseActivity, { timeBean: TimeBean ->
            val min = timeBean.min
            val second = timeBean.second
            LogUtils.e("reciveiTime==$min::$second")
            text = "" + StringUtils.numberToTime(min) + " : " + StringUtils.numberToTime(second)
        })
    }

}